﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TidyBlog.Entity;
using TidyBlog.DAL;

namespace TidyBlog.BLL
{
    /// <summary>
    /// 分类名业务逻辑层
    /// </summary>
    public class CategoryBLL
    {
        /// <summary>
        /// 缓存键名
        /// </summary>
        public const string CacheName = "CategoryList";

        /// <summary>
        /// 验证分类属性
        /// </summary>
        /// <param name="category"></param>
        public static void Validate(Category category)
        {
            if (String.IsNullOrEmpty(category.CategoryName))
            {
                throw new ArgumentException("分类名不能为空");
            }
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="category">分类</param>
        public static void Create(Category category)
        {
            Validate(category);

            try
            {
                CategoryDAL.Create(category);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("创建分类记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 读取分类
        /// </summary>
        /// <param name="categoryId">分类编号</param>
        /// <returns>分类</returns>
        public static Category Read(int categoryId)
        {
            var result = from c in List(0) where c.CategoryId == categoryId select c;

            return result.Count() > 0 ? result.First() : null;
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="category">分类</param>
        public static void Update(Category category)
        {
            Validate(category);

            try
            {
                CategoryDAL.Update(category);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("更新分类记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="category">分类编号</param>
        public static void Delete(int categoryId)
        {
            if (categoryId == 1)
            {
                throw new Exception("不能删除默认分类");
            }

            try
            {
                CategoryDAL.Delete(categoryId);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("删除分类记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="minWeight">最小权重（包括）</param>
        /// <returns>分类列表</returns>
        public static List<Category> List(byte minWeight)
        {
            var list = HttpContext.Current.Cache[CacheName] as List<Category>;

            if (list == null)
            {
                try
                {
                    list = CategoryDAL.List();
                }
                catch
                {
#if DEBUG
                throw;
#else
                    throw new Exception("读取分类记录出错");
#endif
                }

                // 写入缓存
                HttpContext.Current.Cache[CacheName] = list;
            }

            return (from c in list where c.Weight >= minWeight select c).ToList();
        }
    }
}
